<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" ref="dataFormRef" @keyup.enter.native="getDataList()">
      <el-form-item>
        销售日期：
        <el-date-picker v-model="dataForm.queryDates" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期" size="mini" @blur="getDataList()" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        支付类型：
        <el-select size="mini" v-model="dataForm.payType" clearable placeholder="请选择" @change="getDataList()"
          @clear="getDataList()">
          <el-option value="1" label="现金"></el-option>
          <el-option value="2" label="支付宝"></el-option>
          <el-option value="3" label="微信"></el-option>
          <el-option value="4" label="社保卡"></el-option>
          <el-option value="5" label="银联"></el-option>
          <el-option value="6" label="其它"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        营业员：
        <el-select size="mini" @focus="getUsers()" @change='getDataList()' v-model="dataForm.saleUser" clearable placeholder="请选择"
          @clear="getDataList()">
          <el-option v-for="item in users" :key="item.userId" :label="item.username" :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        选择过滤：
        <el-select size="mini" v-model="dataForm.selectField" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in selectFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select size="mini" v-model="dataForm.selectValue" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        数量/价格/金额：
        <el-select size="mini" v-model="dataForm.priceField" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in priceFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select size="mini" v-model="dataForm.priceFormula" clearable placeholder="请选择" @change="getDataList(1)"
          @clear="getDataList(1)">
          <el-option v-for="item in priceFormulas" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input size="mini" v-model="dataForm.priceValue" clearable type="number" placeholder="库存/价格/毛利"
          @blur="getDataList(1)" @clear="getDataList(1)"></el-input>
      </el-form-item>
      <el-form-item>
        主单条件查询：
        <el-select size="mini" v-model="dataForm.queryField" clearable placeholder="请选择" @change="getDataList()"
          @clear="getDataList()">
          <el-option v-for="item in queryFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input size="mini" v-model="dataForm.key" placeholder="参数名" clearable @blur="getDataList()"
          @clear="getDataList()"></el-input>
      </el-form-item>

      <el-form-item>
        明细条件查询：
        <el-select size="mini" v-model="dataForm.queryItemField" clearable placeholder="请选择" @change="getDataList()"
          @clear="getDataList()">
          <el-option v-for="item in queryItemFields" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input size="mini" v-model="dataForm.itemKey" placeholder="参数名" clearable @blur="getDataList()"
          @clear="getDataList()"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button size="mini" type="success" @click="getDataList()" icon="el-icon-search">查询</el-button>
        <el-button @click="resetForm" size="mini" type="warning" icon="el-icon-refresh-left">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form>
      <el-form-item class="button-box">
        <!-- <el-button-group> -->
        <el-button size="mini" v-if="isAuth('sale:saleorder:print')" type="success"  @click="deleteHandle()"
          icon="iconfont icon-print1" >重打小票</el-button>
        <el-button size="mini" v-if="isAuth('sale:saleorder:sale')" type="warning"
          @click="addOrUpdateHandle()" icon="el-icon-plus">再次销售</el-button>

        <!-- </el-button-group>
        <el-button-group> -->
          <el-button size="mini" v-if="isAuth('sale:saleorder:export')" type="primary"  @click="exportData"
            class="iconfont icon-topsales">导出</el-button>
          <el-button size="mini" v-if="isAuth('sale:saleorder:update')" type="danger"  @click="showAuditDialog()"
            class="iconfont icon-account">修改营业员
          </el-button>
          <!-- <el-button size="mini" v-if="isAuth('sale:saleorder:import')" type="primary" @click="importData()"
            class="iconfont icon-skip">导入</el-button> -->
        <!-- </el-button-group> -->
      </el-form-item>
      <el-form-item>
        <el-dialog title="导入数据" :visible.sync="upload.open" width="400px" append-to-body :close-on-click-modal="false">
          <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
            :before-upload="beforeUpload" :action="upload.url" :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false"
            :file-list="excelList" :on-remove="onRemoveDr" :on-change="onChangeDr" drag>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或
              <em>点击上传</em>
            </div>
            <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
          </el-upload>
          <el-button size="mini" type="success" @click="downloadImportTemplate">下载模板</el-button>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitFileForm">导 入</el-button>
            <el-button @click="cancelFileForm">取 消</el-button>
          </div>
        </el-dialog>
      </el-form-item>
    </el-form>

    <!-- 主表 -->
    <el-table :data="dataList" size="mini" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;" @cell-click="tableDbEdit" @cell-dblclick="tableDbEdit"
      :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}" height="320">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="saleNo" header-align="center" align="center" label="销售编号" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="saleType" header-align="center" align="center" label="销售类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.saleType==0" type="danger">退货</el-tag>
          <el-tag v-else type="success">销售</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="saleDate" header-align="center" align="center" label="销售日期" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="backDate" header-align="center" align="center" label="退货日期" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="memberName" header-align="center" align="center" label="会员名称">
      </el-table-column>
      <el-table-column prop="memberNo" header-align="center" align="center" label="会员编号" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="isPending" header-align="center" align="center" label="是否挂单">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPending==1" type="warning">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="pendingRemark" header-align="center" align="center" label="挂单备注" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="totalSaleNum" header-align="center" align="center" label="共购买">
      </el-table-column>
      <el-table-column prop="totalPrediscountFee" header-align="center" align="center" label="折前总额">
      </el-table-column>
      <el-table-column prop="totalMemberFee" header-align="center" align="center" label="会员总额">
      </el-table-column>
      <el-table-column prop="totalDiscountFee" header-align="center" align="center" label="折后总额">
      </el-table-column>
      <el-table-column prop="fundPaySumamt" header-align="center" align="center" label="医保基金">
      </el-table-column>
      <el-table-column prop="toalOtherFee" header-align="center" align="center" label="其它费用">
      </el-table-column>
      <el-table-column prop="paymentFee" header-align="center" align="center" label="实付金额">
      </el-table-column>
      <el-table-column prop="receiveFee" header-align="center" align="center" label="实收金额">
      </el-table-column>
      <el-table-column prop="giveChangeFee" header-align="center" align="center" label="找零金额">
      </el-table-column>
      <el-table-column prop="totalReduceFee" header-align="center" align="center" label="共优惠(元)" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="totalGiveNum" header-align="center" align="center" label="共赠送数量" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="isPayed" header-align="center" align="center" label="是否支付">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPayed==1" type="success">是</el-tag>
          <el-tag v-else type="warning">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="payType" header-align="center" align="center" label="支付类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPayed==1" type="success">现金</el-tag>
          <el-tag v-else-if="scope.row.isPayed==2" type="primary">支付宝</el-tag>
          <el-tag v-else-if="scope.row.isPayed==3" type="warning">微信</el-tag>
          <el-tag v-else-if="scope.row.isPayed==4" type="danger">社保卡</el-tag>
          <el-tag v-else-if="scope.row.isPayed==5" type="info">银联</el-tag>
          <el-tag v-else type="warning">其它</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="saleUser" header-align="center" align="center" label="营业员">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.saleUser" v-if="showInput == `saleUser${scope.row.Id}`"
            @blur='blurInput(scope.row, "saleUser", scope.row.saleUser)' v-focus>
          </el-input>
          <span v-else class="active-input">{{scope.row.saleUser}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="buyerName" header-align="center" align="center" label="购买人">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.buyerName" v-if="showInput == `buyerName${scope.row.Id}`"
            @blur='blurInput(scope.row, "buyerName", scope.row.buyerName)' v-focus>
          </el-input>
          <span v-else class="active-input">{{scope.row.buyerName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="buyerCardId" header-align="center" align="center" label="购买人身份证" width="140" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.buyerCardId" v-if="showInput == `buyerCardId${scope.row.Id}`"
            @blur='blurInput(scope.row, "buyerCardId", scope.row.buyerCardId)' v-focus>
          </el-input>
          <span v-else class="active-input">{{scope.row.buyerCardId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="buyerMobile" header-align="center" align="center" label="购买人手机" width="140" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.buyerMobile" v-if="showInput == `buyerMobile${scope.row.Id}`"
            @blur='blurInput(scope.row, "buyerMobile", scope.row.buyerMobile)' v-focus>
          </el-input>
          <span v-else class="active-input">{{scope.row.buyerMobile}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="singleFee" header-align="center" align="center" label="单张金额">
      </el-table-column>
      <el-table-column prop="backType" header-align="center" align="center" label="退货状态">
       <template slot-scope="scope">
         <el-tag v-if="scope.row.backType==1" type="danger">部分退货</el-tag>
         <el-tag v-else-if="scope.row.backType==3" type="warning">完全退货</el-tag>
         <el-tag v-else type="warning">无退货</el-tag>
       </template>
      </el-table-column>
      <el-table-column prop="orderSource" header-align="center" align="center" label="订单来源">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderSource==1" type="primary">POS</el-tag>
          <el-tag v-else type="success">小程序</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isOnlinePay" header-align="center" align="center" label="线上支付">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isOnlinePay==1" type="primary">线上</el-tag>
          <el-tag v-else type="success">线下</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="storeName" header-align="center" align="center" label="门店名称" v-if="hasRole('role:sys:super:admin')">
      </el-table-column>
      <el-table-column prop="storeCode" header-align="center" align="center" label="门店编码" v-if="hasRole('role:sys:super:admin')">
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="修改时间">
      </el-table-column>
      <el-table-column prop="updateUsername" header-align="center" align="center" label="修改人">
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
        <template slot-scope="scope">
          <span v-html="scope.row.remark"></span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 字表 -->
    <el-table :data="itemDataList" size="mini" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;" @cell-click="tableDbEdit" @cell-dblclick="tableDbEdit" class="item-table" height="250"
      :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="inboundNo" header-align="center" align="center" label="入库单编号" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="productCode" header-align="center" align="center" label="产品编号">
      </el-table-column>
      <el-table-column prop="isDrugPiece" header-align="center" align="center" label="中药饮片">
      </el-table-column>
      <el-table-column prop="gspType" header-align="center" align="center" label="GSP类型">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.gspType" v-if="showInput == `gspType${scope.row.Id}`"
            @blur='blurInput(scope.row, "gspType", scope.row.gspType)' v-focus>
          </el-input>
          <span v-else class="active-input">{{scope.row.gspType}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="pinyin" header-align="center" align="center" label="拼音" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="barCode" header-align="center" align="center" label="条形码" width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="productName" header-align="center" align="center" label="产品名称" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="genericName" header-align="center" align="center" label="通用名称" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="producingArea" header-align="center" align="center" label="产地" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="standard" header-align="center" align="center" label="规格" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="unit" header-align="center" align="center" label="单位">
      </el-table-column>
      <el-table-column prop="approvalNo" header-align="center" align="center" label="批号" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="produceFactory" header-align="center" align="center" label="生产单位" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="dosageForm" header-align="center" align="center" label="剂型" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="expiryDate" header-align="center" align="center" label="有效期" width="110" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="produceDate" header-align="center" align="center" label="生产日期" width="110" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="batchNo" header-align="center" align="center" label="批号" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="lotNo" header-align="center" align="center" label="批次号" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="isRxDrug" header-align="center" align="center" label="是否处方">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isRxDrug==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isEphedrine" header-align="center" align="center" label="含麻黄碱">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isEphedrine==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isGmp" header-align="center" align="center" label="是否GMP">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isGmp==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isSplit" header-align="center" align="center" label="是否拆零药" width="110" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isSplit==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isYiBao" header-align="center" align="center" label="是否医保">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isYiBao==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="stockNum" header-align="center" align="center" label="库存">
      </el-table-column>
      <el-table-column prop="saleNum" header-align="center" align="center" label="销售数量">
      </el-table-column>
      <el-table-column prop="costPrice" header-align="center" align="center" label="成本价">
      </el-table-column>
      <el-table-column prop="costFee" header-align="center" align="center" label="成本金额">
      </el-table-column>
      <el-table-column prop="originalPrice" header-align="center" align="center"label="原价">
      </el-table-column>
      <el-table-column prop="originalFee" header-align="center" align="center" label="原价金额">
      </el-table-column>
      <el-table-column prop="salePrice" header-align="center" align="center" label="出售价">
      </el-table-column>
      <el-table-column prop="saleFee" header-align="center" align="center" label="出售金额">
      </el-table-column>
      <el-table-column prop="retailPrice" header-align="center" align="center" label="指导价">
      </el-table-column>
      <el-table-column prop="retailFee" header-align="center" align="center" label="指导金额">
      </el-table-column>
      <el-table-column prop="tradePrice" header-align="center" align="center" label="批发价">
      </el-table-column>
      <el-table-column prop="tradeFee" header-align="center" align="center" label="批发额">
      </el-table-column>
      <el-table-column prop="memberPrice" header-align="center" align="center" label="会员价">
      </el-table-column>
      <el-table-column prop="memberFee" header-align="center" align="center" label="会员金额">
      </el-table-column>
      <el-table-column prop="discount" header-align="center" align="center" label="折扣">
      </el-table-column>
      <el-table-column prop="discountFee" header-align="center" align="center" label="折扣金额">
      </el-table-column>
      <el-table-column prop="isOnlinePay" header-align="center" align="center" label="线上支付">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isOnlinePay==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="saleType" header-align="center" align="center" label="订单类型">
      </el-table-column>
      <el-table-column prop="hasPromotion" header-align="center" align="center" label="是否促销">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hasPromotion==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="promotionReduceFee" header-align="center" align="center" label="促销减少额" width="120">
      </el-table-column>
      <el-table-column prop="promotionName" header-align="center" align="center" label="促销名称" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="promotionGiveNum" header-align="center" align="center" label="促销赠送数" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="promotionGiveFee" header-align="center" align="center" label="赠品总成本" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="promotionGiveProductNames" header-align="center" align="center" label="促销赠品名称" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="isPausePromotion" header-align="center" align="center" label="暂停促销">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPausePromotion==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="saleUser" header-align="center" align="center" label="营业员">
      </el-table-column>
      <el-table-column prop="isGiveProduct" header-align="center" align="center" label="是否赠品">
      </el-table-column>
      <el-table-column prop="giveProdcutParentCode" header-align="center" align="center" label="赠品对应产品" width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="isCommission" header-align="center" align="center" label="是否提成">
      </el-table-column>
      <el-table-column prop="commissionRadio" header-align="center" align="center" label="提成比例">
      </el-table-column>
      <el-table-column prop="commissionFee" header-align="center" align="center" label="提成金额">
      </el-table-column>
      <el-table-column prop="couponInfo" header-align="center" align="center" label="优惠券">
      </el-table-column>
      <el-table-column prop="isReward" header-align="center" align="center" label="是否奖励">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isReward==1" type="primary">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="rewardState" header-align="center" align="center" label="奖励发放状态" width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.rewardState==1" type="primary">已发放</el-tag>
          <el-tag v-else type="info">未发放</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="rewardFee" header-align="center" align="center" label="奖励金额">
      </el-table-column>
      <el-table-column prop="rewardDate" header-align="center" align="center" label="奖励发放时间" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="修改时间" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="updateUsername" header-align="center" align="center" label="修改人">
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
        <template slot-scope="scope">
          <span v-html="scope.row.remark"></span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './saleorder-add-or-update'
  import {downLoadFile,hasRole} from '@/utils/myUtil.js'
  import { Loading} from "element-ui"
  export default {
    data() {
      return {
        dataForm: {
          queryDates:[],
          key: null,
          queryField: null,
          itemKey: null,
          queryItemField: null,
          payType: null,
          saleUserId: null,
          saleUser: null,
          selectField: null,
          selectValue: null,
          priceField: null,
          priceFormula: null,
          priceValue: null,
        },
        users: [],
        selectFields: [],
        priceFields: [],
        priceFormulas: [],
        units: [],
        supplies: [],
        queryFields: [],
        queryItemFields: [],
        dataList: [],
        itemDataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        showInput: "",
        oldData: {},
        currentData: {},
        //导入
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "导入药品数据",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          // updateSupport: 0,
          // 设置上传的请求头部
          // 上传的地址
          url: `${window.SITE_CONFIG.baseUrl}/sale/saleorder/importData`
        },
        //上传的文件
        excelList: [],
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
      this.initQueryParams()
    },
    methods: {
      // 获取数据列表
      async getDataList() {
        this.dataListLoading = true
        let newDataForm = _.cloneDeep(this.dataForm)
        let queryDates = newDataForm.queryDates || [];
        newDataForm.queryDates = queryDates.join(",")
        const res = await this.$http({
          url: `/sale/saleorder/page`,
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dataForm': newDataForm
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          this.dataList.forEach(item => {
            item.status = item.status == 1 ? true : false
          })
          if (this.dataList.length > 0) {
            this.getItemDataList(this.dataList[0].saleId)
          }else{
            this.itemDataList = []
          }
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      },
      async getItemDataList(saleId) {
        const res = await this.$http({
          url: `/sale/saleorderdetail/listByOrderId`,
          method: 'get',
          params: this.$http.adornParams({
            saleId
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          this.itemDataList = data.list
        } else {
          this.itemDataList = []
        }
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.saleId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$http({
            url: `/sale/saleorder/delete`,
            method: 'delete',
            data: this.$http.adornData(ids, false)
          })
          let data = res.data
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      resetForm() {
        this.$nextTick(() => {
          this.dataForm = this.$options.data().dataForm
          this.getDataList()
        })
      },
      async saveHandle(row) {
        row.status = row.status ? 1 : 0
        const res = await this.$http({
          url: `/sale/saleorder/update`,
          method: 'post',
          data: this.$http.adornData(row)
        });
        let data = res.data
        if (data && data.code !== 0) {
          row.status = !row.status;
          return this.$message.error('修改失败!');
        }
        this.$message.success('更新成功！');
        this.getDataList();
      },
      // 当input失去光标后进行的操作
      async blurInput(row, name, value) {
        // 判断数据是否有所改变，如果数据有改变则调用修改接口
        if (this.oldData[name] != value) {
          row[name] = value
          if (value === true) {
            row[name] = 1
          } else if (value === false) {
            row[name] = 0
          }
        }
        this.showInput = ""
        this.currentData = row
      },
      tableDbEdit(row, column, event) {
        this.showInput = column.property + row.saleId;
        this.oldData[column.property] = row[column.property];
      },
      async exportData() {
        const res = await this.$http({
          url: `/sale/saleorder/exportData`,
          method: 'get',
          responseType: 'blob',
          params: this.$http.adornParams()
        });
        downLoadFile(res)
      },
      beforeUpload(file) {
        //截取文件后缀名
        const fileName = file.name.substring(file.name.lastIndexOf('.'));
        if (fileName.toLowerCase() != '.xls' && fileName.toLowerCase() != '.xlsx') {
          this.$message.error('文件必须为.xls或xlsx类型');
          this.fileList = [];
          return false;
        };
        //读取文件大小
        var fileSize = file.size;
        if (fileSize > 3249576) {
          this.uploadShowDialog = false;
          this.$message({
            type: 'error',
            showClose: true,
            duration: 3000,
            message: '文件不能大于3M!'
          });
          return false;
        }
      },
      // excel表上传
      handleUploadChange(file, fileList) {
        if (fileList.length > 0) {
          // 这一步，是展示最后一次选择的文件
          this.fileList = [fileList[fileList.length - 1]];
        }
      },
      //导入按钮
      handleImport() {
        this.upload.open = true;
      },
      //导入提交
      async submitFileForm() {
        if (this.excelList.length === 0) {
          this.$message.warning("请选择文件");
          return;
        }
        if (this.excelList.length > 1) {
          this.$message.warning("每次只能提交一个文件");
          return;
        }
        let formData = new FormData();
        this.excelList.forEach((item) => {
          formData.append("file", item.raw);
        });
        let loadingInstance = Loading.service({
          lock: true, //lock的修改符--默认是false
          text: "导入中", //显示在加载图标下方的加载文案
          spinner: "el-icon-loading", //自定义加载图标类名
          background: "rgba(0, 0, 0, 0.7)", //遮罩层颜色
          target: document.querySelector("#futureTransferDialog"), //loadin覆盖的dom元素节点
        });
        const res = await this.$http({
          url: this.upload.url,
          method: 'post',
          data: formData
        })
        let data = res.data
        if (data && data.code === 0) {
          loadingInstance.close();
          this.$message({
            type: "success",
            message: "导入成功!",
          });
          this.getDataList();
        }
        this.excelList = [];
        this.upload.open = false;
        loadingInstance.close();
        this.dataListLoading = false
      },
      //下载导入模板
      downloadImportTemplate() {
        window.location.href = "http://file.asdzy.com/upload/files/film/品牌模板.xlsx"
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.excelList = fileList;
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      onChangeDr(file, fileList) {
        this.excelList = fileList;
      },
      onRemoveDr(file, fileList) {
        this.excelList = fileList;
      },
      cancelFileForm() {
        this.upload.open = false
        this.excelList = [];
      },
      async getUsers() {
        const res = await this.$http({
          url: `/sys/user/listUsers`,
          method: 'get',
          params: this.$http.adornParams()
        })
        let data = res.data
        if (data && data.code === 0) {
          this.users = data.data
        }
      },
      setUsers() {
        this.users.forEach((item) => {
          if (this.dataForm.saleUser == item.username) {
            this.dataForm.saleUserId = item.userId;
          }
        });
      },
      initQueryParams() {
        let selectFields = [{
          value: 'sale_type',
          label: '是否退货'
        }, {
          value: 'is_pending',
          label: '是否挂单'
        }, {
          value: 'is_payed',
          label: '是否支付'
        }, {
          value: 'is_online_pay',
          label: '是否线上支付'
        }];

        let priceFields = [{
          value: 'total_sale_num',
          label: '销售总数量'
        }, {
          value: 'total_prediscount_fee',
          label: '折前总金额'
        }, {
          value: 'total_member_fee',
          label: '会员价总金额'
        }, {
          value: 'fund_pay_sumamt',
          label: '医保基金'
        },{
          value: 'toal_other_fee',
          label: '其它费用总额'
        }, {
          value: 'payment_fee',
          label: '实付金额'
        }, {
          value: 'receive_fee',
          label: '实收金额'
        }, {
          value: 'give_change_fee',
          label: '找零金额'
        }, {
          value: 'total_reduce_fee',
          label: '优惠金额'
        }, {
          value: 'total_give_num',
          label: '赠送数量'
        }];

        let priceFormulas = [{
          value: 'GE',
          label: '大于等于'
        }, {
          value: 'EQ',
          label: '等于'
        }, {
          value: 'GT',
          label: '大于'
        }, {
          value: 'LE',
          label: '小于等于'
        }, {
          value: 'LT',
          label: '小于'
        }, {
          value: 'NE',
          label: '不等于'
        }, ]

        let queryFields = [{
          value: 'sale_no',
          label: '销售编码'
        }, {
          value: 'member_mame',
          label: '会员名称'
        }, {
          value: 'member_no',
          label: '会员编号'
        }, {
          value: 'sale_user',
          label: '营业员名称'
        }, {
          value: 'audit_username',
          label: '审核人'
        }]

        let queryItemFields = [{
          value: 'product_code',
          label: '编码'
        }, {
          value: 'bar_code',
          label: '条形码'
        }, {
          value: 'product_name',
          label: '产品名称'
        }, {
          label: '通用名'
        }, {
          value: 'producing_area',
          label: '产地'
        }, {
          value: 'produce_factory',
          label: '厂家'
        }, {
          value: 'dosage_form',
          label: '剂型'
        }, {
          value: 'standard',
          label: '规格'
        }, {
          value: 'approval_no',
          label: '批准文号'
        }]
        this.selectFields = selectFields;
        this.priceFields = priceFields;
        this.priceFormulas = priceFormulas;
        this.queryFields = queryFields;
        this.queryItemFields = queryItemFields;
      }
    },

    directives: {
      // 通过自定义指令实现的表单自动获得光标的操作
      focus: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "input") {
            el.focus()
          } else {
            if (el.getElementsByTagName("input")) {
              el.getElementsByTagName("input")[0].focus()
            }
          }
          el.focus()
        }
      },
      focusTextarea: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "textarea") {
            el.focus()
          } else {
            if (el.getElementsByTagName("textarea")) {
              el.getElementsByTagName("textarea")[0].focus()
            }
          }
          el.focus()
        }
      }
    },
  }
</script>
<style lang="less" scoped>
  .item-table {
    margin-top: 35px;
  }

  .el-select {
    max-width: 100px;
  }

  .el-input {
    margin: 0px;
  }

  .button-box{
    margin-top: 10px;
  }

</style>
